package com.btex.app.tools;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.OTCOrderdetailActivity;
import com.btex.app.R;
import com.btex.app.entity.OTCBuyAndSellOrderBean;
import com.btex.app.entity.OTCBuyBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangyi<zhangyi, 1104745049@qq.com>
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/8/13
 * @time 下午4:48
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */


public class OTCSellConfirmDialog extends Dialog {
    private final Context context;
    @BindView(R.id.otc_dialog_tv_danhao)
    TextView mOtcDialogTvDanhao;
    @BindView(R.id.otc_dialog_tv_name)
    TextView mOtcDialogTvName;
    @BindView(R.id.otc_dialog_tv_number)
    TextView mOtcDialogTvNumber;
    @BindView(R.id.otc_dialog_tv_money)
    TextView mOtcDialogTvMoney;
    @BindView(R.id.otc_dialog_tv_type)
    TextView mOtcDialogTvType;
    @BindView(R.id.otc_dialog_tv_mai)
    TextView mOtcDialogTvMai;
    @BindView(R.id.otc_dialog_tv_ma)
    TextView mOtcDialogTvMa;
    @BindView(R.id.otc_dialog_tv_number_name)
    TextView mOtcDialogTvNumberName;
    @BindView(R.id.otc_dialog_tv_money_name)
    TextView mOtcDialogTvMoneyName;
    private OTCBuyAndSellOrderBean.DataBean.BidsBean mOtcBuyBean;
    @BindView(R.id.otc_dialog_diss)
    TextView mOtcDialogDiss;
    @BindView(R.id.otc_dialog_set)
    TextView mOtcDialogSet;
    private final SharedPreferencesUtil mSp;
    private OTCBuyBean mBuyBean;
    private MicroAVLIDialog mMicroAVLIDialog;


    public OTCSellConfirmDialog(Context context, OTCBuyAndSellOrderBean.DataBean.BidsBean otcBuyBean, OTCBuyBean buyBean) {
        super(context, R.style.coupon_style);
        this.context = context;
        mOtcBuyBean = otcBuyBean;
        mSp = SharedPreferencesUtil.getSharedPreferences(context);
        mBuyBean = buyBean;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otc_confirm_buy_dialog);
        ButterKnife.bind(this);

        mMicroAVLIDialog = new MicroAVLIDialog(context);

        mOtcDialogTvMai.setVisibility(View.GONE);
        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.9);
        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);

        initView();


    }

    private void initView() {


        mOtcDialogTvNumberName.setText("卖出数量:");
        mOtcDialogTvMoneyName.setText("卖出总价:");
        mOtcDialogTvDanhao.setText(mBuyBean.danhao);
        mOtcDialogTvName.setText(mBuyBean.name);
        mOtcDialogTvNumber.setText(mBuyBean.num);
        mOtcDialogTvMoney.setText(mBuyBean.price);

        if (mBuyBean.type.equals("weixin")) {
            mOtcDialogTvType.setText("微信");
        }

        if (mBuyBean.type.equals("alipay")) {
            mOtcDialogTvType.setText("支付宝");
        }


        if (mBuyBean.type.equals("bank")) {
            mOtcDialogTvType.setText("银行卡");
        }

        if (mBuyBean.type.equals("tianya")) {
            mOtcDialogTvType.setText("天涯");
        }


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.otc_dialog_diss, R.id.otc_dialog_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.otc_dialog_diss:
                dismiss();
                break;
            case R.id.otc_dialog_set:

//                context.startActivity(new Intent(context, OTCOrderdetailActivity.class).putExtra("data", (Serializable) mBuyBean));

                getData();


                break;
        }
    }

    private void getData() {

        if (mMicroAVLIDialog != null) {
            mMicroAVLIDialog.show();
        }


        OkGo.<String>post(Config.OTC_SEL).params("order_id", mBuyBean.danhao).params("trade_psw", mBuyBean.password).params("num", mBuyBean.num).params("pay", mBuyBean.type).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (mMicroAVLIDialog != null) {
                    mMicroAVLIDialog.dismiss();
                }
                String result = response.body().toString();

                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if (jsonObject.getString(Config.KEY_CODE).equals("1")) {
                        dismiss();
                        String data = jsonObject.getString("data");
                        context.startActivity(new Intent(context, OTCOrderdetailActivity.class).putExtra("data", data));


                    } else if (jsonObject.getString(Config.KEY_CODE).equals("-8")) {

                        new OTCPaymentMethodsDialog(context).show();
                        dismiss();

                    } else {

//                        SnackbarShow(jsonObject.getString("info"));
                        Util.showSnachBarDialog(mOtcDialogTvMai, context.getResources().getColor(R.color.red), jsonObject.getString("info"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        });


//        String lang = "";
//        boolean isEngilsh = mSp.getBoolean("isEngilsh", false);
//        if (isEngilsh) {
//            lang = "en";
//        } else {
//            lang = "zh_cn";
//        }
//
//
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(context, mSp);
//
//        RequestParams params = new RequestParams();
//        params.addHeader("app-version", Config.VERSION);
//        params.addHeader("channel", MyApplication.mApp.getApplicationMetaData("CXD_CHANNEL_ID"));
//        params.addHeader("my-client", "android");
//        params.addBodyParameter("order_id", mBuyBean.danhao);
//        params.addBodyParameter("trade_psw", mBuyBean.password);
//        params.addBodyParameter("num", mBuyBean.num);
//        params.addBodyParameter("pay", mBuyBean.type);
//        params.addBodyParameter("lang", lang);
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.OTC_SEL, params, new RequestCallBack<String>() {
//
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                String result = responseInfo.result.toString().trim();
//                if (mMicroAVLIDialog != null) {
//                    mMicroAVLIDialog.dismiss();
//                }
//
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//
//                    if (jsonObject.getString(Config.KEY_CODE).equals("1")) {
//                        dismiss();
//                        String data = jsonObject.getString("data");
//                        context.startActivity(new Intent(context, OTCOrderdetailActivity.class).putExtra("data", data));
//
//
//                    } else if (jsonObject.getString(Config.KEY_CODE).equals("-8")) {
//
//                        new OTCPaymentMethodsDialog(context).show();
//                        dismiss();
//
//                    } else {
//
////                        SnackbarShow(jsonObject.getString("info"));
//                        Util.showSnachBarDialog(mOtcDialogTvMai, context.getResources().getColor(R.color.red), jsonObject.getString("info"));
//
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//
//            }
//
//        });


    }


//    private void SnackbarShow(String ms) {
//
//        Snackbar snackbar = ShortSnackbar(mOtcDialogSet, ms, Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//        snackbar.show();
//    }
//
//    public static Snackbar ShortSnackbar(View view, String message, int messageColor, int backgroundColor) {
//        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
//        setSnackbarColor(snackbar, messageColor, backgroundColor);
//        return snackbar;
//    }
//
//    /**
//     * 设置Snackbar文字和背景颜色
//     *
//     * @param snackbar
//     * @param messageColor
//     * @param backgroundColor
//     */
//    public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
//        View view = snackbar.getView();
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            TextView message = (TextView) snackbar.getView().findViewById(R.id.snackbar_text);
//            //View.setTextAlignment需要SDK>=17
//            message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
//            message.setGravity(Gravity.CENTER);
//        }
//
//        if (view != null) {
//            view.setBackgroundColor(backgroundColor);
//            ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
//        }
//    }


}
